package de.java.shortys.myhome.language.controller;

import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.TreeMap;

public class LanguageManager {

	private static Map<String, LanguageManager> instances;

	static {
		instances = new TreeMap<String, LanguageManager>();
	}

	private String name;

	private ResourceBundle bundle;

	public static LanguageManager getInstance(String name) {
		if (instances.get(name) == null) {
			instances.put(name, new LanguageManager(name));
		}

		return instances.get(name);
	}

	public LanguageManager(String name) {
		this.name = "de/java/shortys/myhome/languages/" + name;
		initialize();
	}

	private void initialize() {
		bundle = ResourceBundle.getBundle(name);

	}

	public Locale getCurrentLocale() {
		return bundle.getLocale();
	}

	public void changeLocale(Locale newLocale) {
		bundle = ResourceBundle.getBundle(name, newLocale);
	}

	public String get(String key) {
		return bundle.getString(key);
	}

}
